package de.tum.in.eist.poll.client.gui;

import java.util.Date;

import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.datepicker.client.DateBox;
import com.google.gwt.user.client.ui.TextBox;

public class LectureCreatePanel extends WidgetContainer {
	
	private Label headerLabel;
	private Label topicLabel;
	private Label dateFromLabel;
	private Label dateToLabel;
	private Label errorLabel;
	private DateBox dateFromBox;
	private DateBox dateToBox;
	private TextBox topicTextbox;
	private CheckBox onceCheckBox;
	private CheckBox weeklyCheckBox;
	private Button createButton;
	private FlexTable oncePanel;
	private HorizontalPanel weeklyPanel;
	private HorizontalPanel submitPanel;
	private VerticalPanel mainPanel;
	private boolean minimized;
		
	public LectureCreatePanel() {
		super(false,true);
		init();
		addContent();
	}
	
	public void expand() {
		mainPanel.removeStyleName("closed");
		mainPanel.addStyleName("open");
		mainPanel.setVisible(true);
		minimized = !minimized;
	}
	
	public void collapse() {
		mainPanel.removeStyleName("lectureCreate.open");
		mainPanel.addStyleName("lectureCreate.closed");
		mainPanel.setVisible(false);
		minimized = !minimized;
		// Clear fields
		removeWeeklyPanel();
		dateFromBox.setValue(null);
		dateToBox.setValue(null);
		topicTextbox.setText("");
		onceCheckBox.setValue(true);
		weeklyCheckBox.setValue(false);
	}

	private void addContent() {
		//Header
		oncePanel.setWidget(0, 0, headerLabel);
		oncePanel.getFlexCellFormatter().setColSpan(0, 0, 4);
		// Topic row
		oncePanel.setWidget(1, 0, topicLabel);
		oncePanel.setWidget(1, 1, topicTextbox);
		oncePanel.getFlexCellFormatter().setColSpan(1, 2, 3);
		// Date once row
		oncePanel.setWidget(2, 0, dateFromLabel);
		oncePanel.setWidget(2, 1, dateFromBox);
		oncePanel.setWidget(2, 2, onceCheckBox);
		oncePanel.setWidget(2, 3, weeklyCheckBox);
		// Date weekly row
		weeklyPanel.add(dateToLabel);
		weeklyPanel.add(dateToBox);
		// Submit row
		submitPanel.add(errorLabel);
		submitPanel.add(createButton);
		// Add parts
		mainPanel.add(oncePanel);
		mainPanel.add(weeklyPanel);
		mainPanel.add(submitPanel);
		// Add the main panel with the hole content
		add(mainPanel);
		// Set the panel as minimized at the start - invisble
		mainPanel.setVisible(false);
		weeklyPanel.setVisible(false);
		
		if (!minimized) {
			expand();
		}
	}
	
	private void init() {
		headerLabel = new Label("Create a new lecture");
		topicLabel = new Label("Topic");
		dateFromLabel = new Label("Date from");
		dateToLabel = new Label("Date to");
		errorLabel = new Label();
		dateFromBox = new DateBox();
		dateToBox = new DateBox();
		topicTextbox = new TextBox();
		onceCheckBox = new CheckBox("Once");
		weeklyCheckBox = new CheckBox("Weekly");
		createButton = new Button("Create");
		oncePanel = new FlexTable();
		weeklyPanel = new HorizontalPanel();
		submitPanel = new HorizontalPanel();
		mainPanel = new VerticalPanel();
		minimized = true;
		
		onceCheckBox.setValue(true);
		
		// Add styles
		mainPanel.addStyleName("lectureForm");
		headerLabel.addStyleName("lectureTopic");
		topicLabel.addStyleName("standardLabel");
		topicTextbox.addStyleName("standardTextBox");
		errorLabel.addStyleName("errorLabel");
		dateFromLabel.addStyleName("standardLabel");
		dateToLabel.addStyleName("standardLabel");
		dateFromBox.addStyleName("dateBox");
		dateToBox.addStyleName("dateBox");
		oncePanel.addStyleName("createLectureOncePanel");
		weeklyPanel.addStyleName("createLectureWeeklyPanel");
		submitPanel.addStyleName("createLectureSubmitPanel");
		createButton.addStyleName("standardButton");
		getHideButton().addStyleName("createLectureButton");
	}
	
//	public void clearFields() {
//		collapse();
//		dateFromBox.setValue(null);
//		dateToBox.setValue(null);
//		topicTextbox.setText("");
//		onceCheckBox.setValue(true);
//		weeklyCheckBox.setValue(false);
//	}
	
	/**
	 * add the {@link #weeklyPanel} to the create lecture view
	 */
	public void addWeeklyPanel() {
		weeklyPanel.removeStyleName("lectureCreateWeekly.closed");
		weeklyPanel.addStyleName("lectureCreateWeekly.open");
		//mainPanel.insert(weeklyPanel, 1);
		weeklyPanel.setVisible(true);
	}
	
	/**
	 * remove the {@link #weeklyPanel} from the create lecture view
	 */
	public void removeWeeklyPanel() {
		weeklyPanel.removeStyleName("lectureCreateWeekly.open");
		weeklyPanel.addStyleName("lectureCreateWeekly.closed");
		weeklyPanel.setVisible(false);
	}

	public Date getDateFrom() {
		return dateFromBox.getValue();
	}

	public Date getDateTo() {
		return dateToBox.getValue();
	}

	public String getTopic() {
		return topicTextbox.getText();
	}

	public CheckBox getOnceCheckBox() {
		return onceCheckBox;
	}

	public CheckBox getWeeklyCheckBox() {
		return weeklyCheckBox;
	}

	public Button getCreateButton() {
		return createButton;
	}

	public Label getErrorLabel() {
		return errorLabel;
	}
	
	public boolean isMinimized() {
		return minimized;
	}
}
